/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 苏西西
 * Date: 2023-07-05
 * Time: 8:43
 */
public class MySingleList {
    static class Node{
        public int val;//存储的数据
        public Node next;//存储下一个节点的地址

        public Node(int val){
            this.val = val;

        }
    }

    public Node head;//代表当前链表的头节点的引用


    public void createLink(){
        Node node1 = new Node(12);
        Node node2 = new Node(45);
        Node node3 = new Node(23);
        Node node4 = new Node(90);
        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        head = node1;


    }

    /**
     * 遍历链表
     */
    public void display(){
        //如果说 把整个链表都 遍历完成，就需要head == null
        //如果遍历到链表的尾部，则是head.next == null\
        Node cur = head;
        while (cur != null){
            System.out.print(cur.val+" ");
            cur = cur.next;

        }
        System.out.println();

    }
    //查找是否包含关键字key 是否在单链表之中
    public boolean contains(int key){
        Node cur = head;
        while (cur != null){
            if(cur.val == key){
                return true;
            }
            cur = cur.next;
        }
        return  false;
    }
    //得到链表的长度
    public int size(){
        int count = 0;
        Node cur = head;
        while(cur != null){
            count ++;
            cur = cur.next;
        }
        return count;
    }


    //头插法
    public void addFirst(int data){
        Node node= new Node(data);
        node.next = head;
        head = node;
    }
    //尾插法

    public void addEnd(int data){
        Node node = new Node(data);
        if(head == null){
            head = node;
            return;
        }
        Node cur = head;
        while (cur.next != null){
            cur = cur.next;
        }
        cur.next = node;
    }

    //任意位置插入，第一个数据节点为0号下标
    public void addIndex (int index,int data) throws IndexOutOfException{
        checkIndex(index);
        if(index == 0){
            addFirst(data);
            return;
        }
        if(index == size()){
            addEnd(data);
            return;
        }
        Node cur = findIndexSubone(index);
        Node node = new Node(data);
        node.next = cur.next;
        cur.next = node;
    }

    /**
     * 找到index-1 位置节点的地址
     * @param index
     * @return
     */
    private Node findIndexSubone(int index){
        Node cur = head;
        int count = 0;
        while(count !=index -1){
            cur = cur.next;
            count++;
        }
        return  cur;
    }

    private void checkIndex(int index){
        if(index < 0 || index > size() ){
            throw new IndexOutOfException("index位置不合法");
        }

    }

    //删除第一次出现关键字为key的节点
    public void remove (int key){
        if(head == null){
            return;//一个节点都没有
        }
        if(head.val == key){
            head = head.next;
            return;
        }

        Node cur = searchPrev(key);
        if(cur == null){
            return;
        }
        Node del = cur.next;//要删除的节点
        cur.next = del.next;
    }

    /**
     * 找到关键字key的前一个节点
     * @param key
     * @return
     */
    private  Node searchPrev(int key){
        if(head == null){
            return null;//一个节点都没有
        }
        Node cur = head;
        while(cur.next !=  null){
            if(cur.next.val == key){
                return  cur;
            }
            cur = cur.next;
        }
        return  null;//没有你要删除的节点
    }

    //删除所有key的节点
    public void removeAllKey(int key){
        if(head == null){
            return;
        }
        Node prev = head;
        Node cur = head.next;
        while (cur != null){
            if(cur.val == key){
                prev.next = cur.next;
                cur = cur.next;
            }else {
                prev = cur;
                cur = cur.next;
            }
            if(head.val == key){
                head = head.next;
            }
        }

    }
    public void clear(){
        head = null;

    }

}
